package com.hengtongxing.hejiayun.login;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.hengtongxing.hejiayun.MainActivity;
import com.hengtongxing.hejiayun.R;
import com.hengtongxing.hejiayun.base.BaseActivity;
import com.hengtongxing.hejiayun.base.DataCallBack;
import com.hengtongxing.hejiayun.bean.AppStartPicBean;
import com.hengtongxing.hejiayun.dialog.PrivacyDialog;
import com.hengtongxing.hejiayun.homepage.model.HomePageModelImpl;
import com.hengtongxing.hejiayun.utils.GlideUtils;
import com.hengtongxing.hejiayun.utils.SPManager;

import java.util.Timer;
import java.util.TimerTask;

import butterknife.BindView;
import butterknife.ButterKnife;


public class SplashActivity extends BaseActivity {
    @BindView(R.id.man_tv)
    TextView manTv;
    @BindView(R.id.iv_picture)
    ImageView ivPicture;
    private Handler handler;
    private Runnable runnable;
    Timer timer = new Timer();
    private int recLen = 3;//跳过倒计时提示3秒

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        ButterKnife.bind(this);
        requestAppStartPic();
        handler = new Handler();
        if (TextUtils.isEmpty(SPManager.getInstance().getProtocol())) {
            PrivacyDialog dialog = PrivacyDialog.newInstance();
            dialog.show(getSupportFragmentManager(), "dialog");
        } else {
            onclick();
            timer.schedule(task, 1000, 1000);//等待时间一秒，停顿时间一秒
            handler.postDelayed(runnable = new Runnable() {
                @Override
                public void run() {
                    if (!TextUtils.isEmpty(SPManager.getInstance().getToken()) && !TextUtils.isEmpty(SPManager.getInstance().getUserInfoJson())) {
                        Intent intent = new Intent(SplashActivity.this, MainActivity.class);
                        startActivity(intent);
                    } else {
                        Intent intent = new Intent(SplashActivity.this, LoginActivity.class);
                        startActivity(intent);
                    }
                    finish();
                }
            }, 3000);
        }
    }

    public void requestAppStartPic() {
        new HomePageModelImpl().requestAppStartPic(new DataCallBack<AppStartPicBean>() {
            @Override
            public void onSuccessful(AppStartPicBean bean) {
                if (bean.getData() != null) {
                    manTv.setVisibility(View.VISIBLE);
                    GlideUtils.loadImage(mActivity, ivPicture, bean.getData().getPic());
                }
            }

            @Override
            public void onFailed(String msg, String code) {

            }
        });
    }

    private void onclick() {
        manTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!TextUtils.isEmpty(SPManager.getInstance().getToken()) && !TextUtils.isEmpty(SPManager.getInstance().getUserInfoJson())) {
                    Intent intent = new Intent(SplashActivity.this, MainActivity.class);
                    startActivity(intent);
                } else {
                    Intent intent = new Intent(SplashActivity.this, LoginActivity.class);
                    startActivity(intent);
                }
                finish();
                if (runnable != null) {
                    handler.removeCallbacks(runnable);
                }
            }
        });
    }

    TimerTask task = new TimerTask() {
        @Override
        public void run() {
            runOnUiThread(new Runnable() { // UI thread
                @Override
                public void run() {
                    recLen--;
                    manTv.setText("跳过 " + recLen + "s");
                    if (recLen < 0) {
                        timer.cancel();
                        manTv.setVisibility(View.GONE);//倒计时到0隐藏字体
                    }
                }
            });
        }
    };

}
